﻿using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Qdrant.Client.Grpc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QdrantConnector
{
    [Icon("pack://application:,,,/QdrantConnector;component/Resources/Icon.png")]
    [Designer("QdrantConnector.Designer.QdrantConnectorServerCommandDesigner, QdrantConnector")]
    [Category("Qdrant向量数据库")]
    [OrderWeight(820)]
    public class HasCollection : BaseQdrantOp
    {
        [DisplayName("将检查结果保存到变量")]
        [OrderWeight(999)]
        [ResultToProperty]
        public string ResultInParamater { get; set; } = "IsExist";

        public override async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var client = await getQrantClient(dataContext);
            var collectionName = await getCollectionName(dataContext);
            
            dataContext.Log.AppendLine($"开始检查Qdrant集合【{collectionName}】是否存在...");
            var tick = DateTime.Now;

            var exits = await client.CollectionExistsAsync(
                 collectionName: await getCollectionName(dataContext)
             );

            dataContext.Log.AppendLine($"Qdrant检查集合【{collectionName}】是否存在，结果：{exits}，耗时：{(DateTime.Now - tick).TotalMilliseconds}ms");

            // 将结果保存到指定的变量中
            if (!string.IsNullOrEmpty(ResultInParamater))
            {
                dataContext.Parameters[ResultInParamater] = exits;
            }

            return new ExecuteResult();
        }

        public override string ToString()
        {
            return "Qdrant检查集合是否存在";
        }
    }


}
